<template>
  <!-- 号源 -->
  <section>
    <el-form :inline="true" @submit.native.prevent="getData(-1)">
      <el-form-item>
        <deptSearch v-model="form.deptId" @change="onChangeDept"></deptSearch>
      </el-form-item>
      <el-form-item>
        <doctorSearch ref="doctorSearch" v-model="form.doctorId" :deptId="form.deptId"></doctorSearch>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="form.dateDay" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit" circle icon="el-icon-search"></el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :disabled="!qxBtns.edit" plain @click="editSource=true">+加号</el-button>
      </el-form-item>
    </el-form>
    <table class="table-box">
      <thead>
        <tr>
          <th>医生</th>
          <th>科室</th>
          <th>时间</th>
          <th>状态</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>{{item.doctorName}}</td>
          <td>{{item.deptName}}</td>
          <td>
            <div>{{item.dateDay}}</div>
            <div>{{item.startTime}}~{{item.endTime}}</div>
          </td>
          <td>
            <el-badge v-if="item.medicalCard" value="已预约"></el-badge>
            <el-badge v-else value="空闲" type="success"></el-badge>
          </td>
          <td>
            <el-link v-if="qxBtns.del&&!item.medicalCard" type="danger" @click="onDel(item,index)">删除</el-link>
          </td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br>
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <editSource v-model="editSource" @success="getData"></editSource>
  </section>
</template>
<script>
export default { 
  components: {
    deptSearch: () => import("@/pages/_com/deptSearch.vue"),
    doctorSearch: () => import("@/pages/_com/doctorSearch.vue"),
    editSource: () => import("./_com/editSource.vue")
  },
  data() {
    return {
      tableList: [],
      editSource: false,
      form: {
        doctorId: "",
        deptId: "",
        hpId: "",
        dateDay: "",
        status: 1
      },
      pager: { pageSize: 20, pageNum: 1, total: 0 },
      qxBtns: {}
    };
  },
  created() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.getQX();
    th.getData();
  },
  methods: {
    onDel(obj, index) {
      let th = this;
      th.$confirm(`确定删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          let j = { sourceIds: obj.id };
          th.showLoading();
          th.$api.delSources(j).then(res => {
            if (res.executed) {
              th.$message.success("删除成功");
              th.tableList.splice(index, 1);
              th.pager.total--;
            } else {
              th.$message.error(res.message);
            }
            th.showLoading(false);
          });
        })
        .catch(() => {
          th.showLoading(false);
        });
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.total = 0;
        th.pager.pageNum = 1;
      }
      let j = {};
      Object.assign(j, th.form, th.pager);
      th.$api.getSourcesList(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
      });
    },
    onChangeDept() {
      let th = this;
      th.form.doctorId = "";
      th.$nextTick(function() {
        th.$refs["doctorSearch"].getData();
      });
    }
  }
};
</script>